<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="讲师名称">
        <el-input v-model="teacher.name"/>
      </el-form-item>
      <el-form-item label="讲师排序">
        <el-input-number v-model="teacher.sort" controls-position="right"/>
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-select v-model="teacher.level" clearable placeholder="请选择">
          <!--
            数据类型一定要和取出的json中的一致，否则没法回填
            因此，这里value使用动态绑定的值，保证其数据类型是number
          -->
          <el-option :value="1" label="高级讲师"/>
          <el-option :value="2" label="首席讲师"/>
        </el-select>
      </el-form-item>
      <el-form-item label="讲师资历">
        <el-input v-model="teacher.career"/>
      </el-form-item>
      <el-form-item label="讲师简介">
        <el-input v-model="teacher.intro" :rows="10" type="textarea"/>
      </el-form-item>

      <!-- 讲师头像开始-->
      <el-form-item label="讲师头像">
        <!-- 头衔缩略图 -->
        <pan-thumb :image="teacher.avatar"/>
        <!-- 文件上传按钮 -->
        <el-button type="primary" icon="el-icon-upload" @click="imagecropperShow=true">更换头像</el-button>

        <!--
    v-show：是否显示上传组件
    :key：类似于id，如果一个页面多个图片上传控件，可以做区分
    :url：后台上传的url地址
    @close：关闭上传组件
    @crop-upload-success：上传成功后的回调
    field="file" 相当于type="file"-->
        <image-cropper
          v-show="imagecropperShow"
          :width="300" :height="300"
          :key="imagecropperKey"
          :url="BASE_API+'/eduOss/uploadFile'"
          field="file"
          @close="close"
          @crop-upload-success="cropSuccess"/>
      </el-form-item>
      <!-- 讲师头像结束-->

      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {saveTeacher, getTeacherInfo, updateTeacher} from "@/api/edu/teacher";
import PanThumb from "@/components/PanThumb";
import ImageCropper from "@/components/ImageCropper";

export default {
  name: "save",
  components: {PanThumb, ImageCropper},
  data() {
    return {
      teacher: {
        name: '',
        sort: 0,
        level: 2,
        career: '',
        intro: '',
        avatar: 'https://works-files.oss-cn-shenzhen.aliyuncs.com/image/default-avatar.png',
      },
      saveBtnDisabled: false,//保存按钮是否可以点击（防止重复提交）
      BASE_API: process.env.BASE_API,// 接口API地址,获取dev.env.js里的地址
      imagecropperShow: false,//上传弹框的组件是否显示
      imagecropperKey: "imagecropperKey000",//上传组件的key，解决添加用户时上传完毕，点击更换头像显示上传成功的问题
    }
  },
  created() {
    this.init();
  },
  watch: {
    $route(to, from) {
      this.init();
    }
  },
  methods: {
    //页面初始化方法，根据路径判断是添加还是更新
    init() {
      const userId = this.$route.params.id;
      if (userId) {
        this.getTeacherInfo(userId);
      } else {
        this.initData();
      }
    },
    //初始化data;
    initData() {
      Object.assign(this.$data, this.$options.data());
    },
    //添加或修改讲师信息
    saveOrUpdate() {
      this.saveBtnDisabled = true;
      //判断techer中id是否有值，有值为修改，没有值为删除
      if (this.teacher.id) {
        this.updateTeacher();
      } else {
        this.saveTeacher();
      }
    },
    //添加讲师
    saveTeacher() {
      saveTeacher(this.teacher).then(ser => {
        this.initData();
        this.$message({
          type: 'success',
          message: '添加成功'
        });
        this.$router.push('/teacher/index')
      }).catch(err => {
        this.saveBtnDisabled = false;
      });
    },
    //根据讲师id查询讲师信息
    getTeacherInfo(id) {
      getTeacherInfo(id).then(ser => {
        this.teacher = ser.data.teacher;
      });
    },
    //修改讲师
    updateTeacher() {
      updateTeacher(this.teacher).then(ser => {
        this.initData();
        this.$message({
          type: 'success',
          message: '修改成功'
        });
        this.$router.push('/teacher/index')
      }).catch(err => {
        this.saveBtnDisabled = false;
      });
    },
    //关闭上传弹框的组件
    close() {
      this.imagecropperShow = false;
    },
    //上传成功后的操作
    cropSuccess(data) {
      this.imagecropperShow = false;
      this.teacher.avatar = data.url;
      this.imagecropperKey = "imagecropperKey" + Math.floor(Math.random() * 100);
      this.$message({
        type: 'success',
        message: '上传成功'
      });
    },
  }

}
</script>

<style scoped>

</style>
